//
//  HYVIPCharts.m
//  HaiYue
//
//  Created by tucici on 2018/6/18.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYVIPCharts.h"
CGFloat const CHARTSCELLHEIGHT = 40;
@implementation HYVIPCharts
+ (UIImage *)chartsWithArray:(NSArray *)array
{
    if (array.count == 0)
    {
        return nil;
    }
    NSArray *title_Arr = @[@"VIP等级",@"VIP折扣",@"达成条件"];
    int row = (int)array.count + 1;//行数
    int column = (int)title_Arr.count ;//列数
    CGFloat total_H = CHARTSCELLHEIGHT * row + GAP + 1;//图表总高度
    UIFont *title_Font = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightBold];
    UIFont *item_Font = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight];
    
    
    //获取上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(SCREEN_WIDTH, total_H), NO, 0.0);
    
    //画横线
    for (int i = 0; i < row; i++)
    {
        [self drawLineColor:THEME_COLOR startPoint:CGPointMake(GAP, GAP + i * CHARTSCELLHEIGHT) endPoint:CGPointMake(SCREEN_WIDTH - GAP, GAP + i * CHARTSCELLHEIGHT)];
        //最后一行横线
        if (i == row - 1)
        {
            [self drawLineColor:THEME_COLOR startPoint:CGPointMake(GAP, GAP + (i + 1) * CHARTSCELLHEIGHT) endPoint:CGPointMake(SCREEN_WIDTH - GAP, GAP + (i + 1) * CHARTSCELLHEIGHT)];
        }
    }
    
    //画竖线
    CGFloat column_stratX = GAP;
    for (int j = 0; j < column + 1; j ++)
    {
        if (j == 1 || j == 2)
        {
            column_stratX = [title_Arr[j - 1] sizeWithAttributes:@{NSFontAttributeName :title_Font}].width + GAP * 2+ column_stratX;
        }
        else if(j == column)//最后一列竖线
        {
            column_stratX = SCREEN_WIDTH - GAP;
        }
        [self drawLineColor:THEME_COLOR startPoint:CGPointMake(column_stratX, GAP) endPoint:CGPointMake(column_stratX, total_H)];
    }
    
    //画text
    CGRect rect = CGRectZero;
    CGFloat column_originX = GAP;
    for (int m = 0; m < column; m ++)
    {
        CGFloat column_width = (m == 0 || m == 1)?
        ([title_Arr[m] sizeWithAttributes:@{NSFontAttributeName :title_Font}].width + GAP * 2):
        (SCREEN_WIDTH - GAP - column_originX);
        
        //画title
        NSString *title = title_Arr[m];
        rect = CGRectMake(column_originX, GAP, column_width, CHARTSCELLHEIGHT);
        [self drawText:title rect:rect font:title_Font color:THEME_COLOR];
        
        column_originX = column_width + column_originX;
    }
    //end
    
    //画内容
    CGFloat column_width_1 = [@"VIP等级" sizeWithAttributes:@{NSFontAttributeName :title_Font}].width + GAP * 2;
    CGFloat column_width_2 = [@"VIP折扣" sizeWithAttributes:@{NSFontAttributeName :title_Font}].width + GAP * 2;
    CGFloat column_width_3 = SCREEN_WIDTH - column_width_1 - column_width_2 - GAP * 2;
    CGFloat column_originX_1 = GAP;
    CGFloat column_originX_2 = column_originX_1 + column_width_1;
    CGFloat column_originX_3 = column_originX_2 + column_width_2;
    
    for (int n = 0; n < array.count; n++)
    {
        CGFloat column_originY = GAP + CHARTSCELLHEIGHT * (n + 1);
        NSString *vip = [[array[n] allKeys] firstObject];
        NSString *discout = [[[array[n] allValues] firstObject] valueForKey:@"discount"];
        NSString *content = [[[array[n] allValues] firstObject] valueForKey:@"content"];
        
        CGRect vip_rect = CGRectMake(column_originX_1, column_originY, column_width_1, CHARTSCELLHEIGHT);
        CGRect discount_rect = CGRectMake(column_originX_2,column_originY, column_width_2, CHARTSCELLHEIGHT);
        CGRect content_rect = CGRectMake(column_originX_3, column_originY, column_width_3, CHARTSCELLHEIGHT);
        
        [self drawText:vip rect:vip_rect font:item_Font color:TEXT_BLACK_COLOR];
        [self drawText:discout rect:discount_rect font:item_Font color:TEXT_BLACK_COLOR];
        [self drawText:content rect:content_rect font:item_Font color:TEXT_BLACK_COLOR];
    }
    //end
    
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

//画线
+ (void)drawLineColor:(UIColor *)color startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{
    
    UIBezierPath *path = [UIBezierPath  bezierPath];
    path.lineWidth = 0.5;
    [color set];
    [path moveToPoint:startPoint];
    [path addLineToPoint:endPoint];
    [path stroke];
    
}

//
+ (void)drawText:(NSString *)text rect:(CGRect)rect font:(UIFont *)font color:(UIColor *)color
{
    //算出文本size
    CGSize text_Size = [text sizeWithAttributes:@{NSFontAttributeName:font}];
    //算出文本横向偏倚和纵向偏移
    CGFloat text_width_gap = (rect.size.width - text_Size.width) / 2.0;
    CGFloat text_height_gap = (rect.size.height - text_Size.height) / 2.0;
    //算出文本rect
    CGRect text_rect = CGRectMake(rect.origin.x + text_width_gap, rect.origin.y + text_height_gap, rect.size.width, rect.size.height);
    //画文本
    [text drawInRect:text_rect withAttributes: @{ NSFontAttributeName :font, NSForegroundColorAttributeName :color}];
}


@end
